package borge.web.teamspirit.client.page.team.widgets;

import borge.web.teamspirit.client.SessionData;
import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.TeamSpiritConstants;
import borge.web.teamspirit.client.data.TeamPlayer;
import borge.web.teamspirit.client.interfaces.ServerCallbackHandler;
import borge.web.teamspirit.client.interfaces.PersistentData.DataKey;
import borge.web.teamspirit.client.page.shared.widgets.ShowDataWidget;
import borge.web.teamspirit.client.page.team.TokenData;
import borge.web.teamspirit.client.servercomm.DBQuery;
import borge.web.teamspirit.client.servercomm.DBQueryResults;
import borge.web.teamspirit.client.servercomm.ServerComm;
import borge.web.teamspirit.client.servercomm.DBQueryResults.Result;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ShowPlayerWidget extends ShowDataWidget {

	// what do we want to show here?
	// just name of person, and then the player info
	// player stats in this team
	// link to person profile
	
	private VerticalPanel iPanel;
	private FlexTable iTable;
	private TeamPlayer iPlayer;
	private String iNewPlayerKey;
	
	public ShowPlayerWidget(){
		super(null);
		
		//setPlayer(aPlayer);
		
		iTable = new FlexTable();
		iPanel = new VerticalPanel();
		iPanel.add(iTable);
		//if(iPlayer != null)
			//createTable();
		
		initWidget(iPanel);
	}
	
	public void setPlayer(TeamPlayer aPlayer){
		iPlayer = aPlayer;
	}
	
	public void createTable(){
		
		int row = 0, column = 0;
		
		TeamSpiritConstants tsc = TeamSpirit.getConstants();
		
		iTable.setText(row, column++, tsc.name());
		iTable.setText(row++, column--, iPlayer.getPersonName());
		
		iTable.setText(row, column++, tsc.active());
		iTable.setText(row++, column--, iPlayer.isActive().toString());
		
		iTable.setText(row, column++, tsc.join_date());
		iTable.setText(row++, column--, iPlayer.getJoinDate().toString());
		
		iTable.setText(row, column++, tsc.position());
		iTable.setText(row++, column--, iPlayer.getPosition());
		
		iTable.setText(row, column++, tsc.shirt_number());
		iTable.setText(row++, column--, iPlayer.getShirtNumber());
		
		iTable.setText(row, column++, tsc.other_info());
		iTable.setText(row++, column--, iPlayer.getOtherInfo());
		
		String token = TokenData.assembleTeamToken(TokenData.SHOW, TokenData.PERSON, iPlayer.getPersonKey());
		iTable.setWidget(row++, column, new Hyperlink("View user profile", token));
		
	}

	@Override
	public boolean needsUpdate(String aData) {

		TeamPlayer newPlayer = SessionData.sCurrentTeam.getPlayerByKey(aData);

		// If we have a player, just set it and update
		if(newPlayer != null){
			iPlayer = newPlayer;
			createTable();
			return false;
		}
		
		// If player is already the one requested
		if(iPlayer != null && iPlayer.getEncodedKey().equals(aData)){
			return false;
		}
		
		// If none of the above, we must get from db
		iNewPlayerKey = aData;
		return true;
	}

	@Override
	public void populateData(DBQueryResults results) {

		Result result = results.getResult(DataKey.TEAMPLAYER);
		
		if(result != null){
			iPlayer = (TeamPlayer)result.getResultList().get(0);
		}
		
		createTable();
	}

	@Override
	public void requestServerData(ServerCallbackHandler aHandler) {
		DBQuery query = new DBQuery(DataKey.TEAMPLAYER, TeamPlayer.class.getName());
		query.addFilter("iEncodedKey", DBQuery.EQUALS, "keyParam", "String", iNewPlayerKey, null);
		ServerComm.getObjectList(query, aHandler);
	}
	
	
	
}